package com.example.buttomnavigationapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Switch;

import com.example.buttomnavigationapplication.base.BaseActivity;
import com.example.buttomnavigationapplication.fragment.FriendFragment;
import com.example.buttomnavigationapplication.fragment.MeasuringFragment;
import com.example.buttomnavigationapplication.fragment.MineFragment;
import com.example.buttomnavigationapplication.fragment.ReportFragment;
import com.example.buttomnavigationapplication.fragment.ShopFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private FrameLayout flContent;
    private BottomNavigationView navigation;
    private int preFragment = 0;// 记录上一个被点击的 fragment页面 ，默认值是0
    private List<Fragment> fragments;
    @Override
    protected void initData() {
        initview();
        fragments=new ArrayList<>();
        fragments.add(new MeasuringFragment());
        fragments.add(new ReportFragment());
        fragments.add(new ShopFragment());
        fragments.add(new FriendFragment());
        fragments.add(new MineFragment());
        //默认显示第一个fragment
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content,fragments.get(0)).commit();
        navigation.setOnNavigationItemSelectedListener(this);
    }
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.nav_measuring:
                if(preFragment!=0){
                    switchFragemnt(preFragment,0);
                    preFragment=0;
                }
                return true;
            case R.id.nav_Dailyreport:
                if(preFragment!=1){
                    switchFragemnt(preFragment,1);
                    preFragment=1;
                }
                return true;

            case R.id.nav_shop:
                if(preFragment!=2){
                    switchFragemnt(preFragment,2);
                    preFragment=2;
                }
                return true;
            case R.id.nav_friend:
                if(preFragment!=3){
                    switchFragemnt(preFragment,3);
                    preFragment=3;
                }
                return true;
            case R.id.nav_mine:
                if(preFragment!=4){
                    switchFragemnt(preFragment,4);
                    preFragment=4;
                }
                return true;
        }
        return false;
    }

    private void switchFragemnt(int preFragment, int i) {
        FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
        ft.hide(fragments.get(preFragment));
        //如果当前被点击的fragment页面还没有被加入到   FragmentManager 里面，则需要添加进来
        if(fragments.get(i).isAdded()==false){
            ft.add(R.id.fl_content,fragments.get(i));
        }
        ft.show(fragments.get(i)).commitAllowingStateLoss();
    }

    @Override
    protected int initLayoutResource() {
        return R.layout.activity_main;
    }

    //实例化
    private void initview() {
        flContent=findViewById(R.id.fl_content);
        navigation= findViewById(R.id.navigation);

    }

}